package org.luxor.commons.zookeeper.config.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.List;

/**
 * Zookeeper 全局配置
 *
 * @author Mr.Yan  @date: 2020/12/14
 */
@ConfigurationProperties(prefix = ZookeeperProperties.PREFIX)
public class ZookeeperProperties {
    public static final String PREFIX = "spring.zookeeper";

    private final Boolean DEFAULT_ENABLED = Boolean.TRUE;
    // 根节点路径
    public static final String ROOT_PATH = "/";
    // 授权方式
    private final String DEFAULT_SCHEME = "digest";
    // 服务集地址
    private final String DEFAULT_HOST = "localhost:2181";
    // 会话超时时间(60秒)
    private final Integer DEFAULT_SESSION_TIMEOUT_MS = 60000;
    // 连接超时时间(15秒)
    private final Integer DEFAULT_CONNECTION_TIMEOUT_MS = 15000;
    // 重试策略：尝试30次
    private final Integer RETRY_CONN_COUNT = 30;
    // 重试策略：每次间隔5秒
    private final Integer RETRY_CONN_INTERVAL = 5000;
    // 独立命名空间
    private final String DEFAULT_NAMESPACE = "luxor";

    /**
     * 是否启用
     */
    private boolean enabled;

    /**
     * Zookeeper server host. (localhost:2181,172.168.1.204:2181,172.168.1.205:2181)
     */
    private String host;

    /**
     * Zookeeper server scheme.
     */
    private String scheme;

    /**
     * Zookeeper server auth.
     */
    private String auth;

    /**
     * 会话超时时间(单位：毫秒，默认60秒)
     */
    private int sessionTimeout;
    /**
     * 连接超时时间(单位：毫秒，默认15秒)
     */
    private int connectionTimeout;
    /**
     * 重试策略，尝试连接多少次？
     */
    private int retryConnCount;
    /**
     * 重试策略，每次连接间隔多少秒？
     */
    private int retryConnInterval;

    /**
     * 独立命名空间(非必填，默认值:luxor)
     */
    private String nameSpace;

    /**
     * 监听忽略列表
     */
    private List<String> listenerIgnores = new ArrayList<String>() {
        private static final long serialVersionUID = 5281239410190516462L;

        {
            add("/kafka");
            add("/zookeeper");
        }

    };

    public ZookeeperProperties() {
        this.enabled = DEFAULT_ENABLED.booleanValue();
        this.host = DEFAULT_HOST;
        this.sessionTimeout = DEFAULT_SESSION_TIMEOUT_MS;
        this.connectionTimeout = DEFAULT_CONNECTION_TIMEOUT_MS;
        this.retryConnCount = RETRY_CONN_COUNT;
        this.retryConnInterval = RETRY_CONN_INTERVAL;
        this.nameSpace = DEFAULT_NAMESPACE;
        this.scheme = DEFAULT_SCHEME;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getScheme() {
        return scheme;
    }

    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    public String getAuth() {
        return auth;
    }

    public void setAuth(String auth) {
        this.auth = auth;
    }

    public String getNameSpace() {
        return nameSpace;
    }

    public void setNameSpace(String nameSpace) {
        this.nameSpace = nameSpace;
    }

    public int getSessionTimeout() {
        return sessionTimeout;
    }

    public void setSessionTimeout(int sessionTimeout) {
        this.sessionTimeout = sessionTimeout;
    }

    public int getConnectionTimeout() {
        return connectionTimeout;
    }

    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public int getRetryConnCount() {
        return retryConnCount;
    }

    public void setRetryConnCount(int retryConnCount) {
        this.retryConnCount = retryConnCount;
    }

    public int getRetryConnInterval() {
        return retryConnInterval;
    }

    public void setRetryConnInterval(int retryConnInterval) {
        this.retryConnInterval = retryConnInterval;
    }

    public List<String> getListenerIgnores() {
        return listenerIgnores;
    }

    public void setListenerIgnores(List<String> listenerIgnores) {
        this.listenerIgnores = listenerIgnores;
    }

    public void addListenerIgnores(String nodePath) {
        this.listenerIgnores.add(nodePath);
    }

    public void removeListenerIgnores(String nodePath) {
        this.listenerIgnores.remove(nodePath);
    }
}
